package edu.scu.declaresys.service.impl;

import java.util.regex.Pattern;

import org.apache.commons.codec.digest.DigestUtils;

import edu.scu.declaresys.config.Configs;
import edu.scu.declaresys.dao.DAOFactory;
import edu.scu.declaresys.dao.intr.IUserDAO;
import edu.scu.declaresys.dao.util.Depth;
import edu.scu.declaresys.exception.ExecuteException;
import edu.scu.declaresys.service.intr.IUserService;
import edu.scu.declaresys.util.PageList;
import edu.scu.declaresys.vo.Area;
import edu.scu.declaresys.vo.User;

public class UserServiceImpl implements IUserService
{
	private IUserDAO dao = DAOFactory.createUserDAO();
	
	
	@Override
	public User add(User vo) throws ExecuteException
	{
		vo.setPswd(DigestUtils.md5Hex(vo.getPswd()));
		Integer id = dao.save(vo);
		return dao.findById(id, Depth.ONE);
	}

	@Override
	public void updateBasicInfo(User vo, Integer id) throws ExecuteException
	{
		vo.setCountyId(null);
		vo.setCityId(null);
		vo.setProvinceId(null);
		vo.setLevel(null);
		vo.setLoginName(null);
		if(vo.getPswd() != null)
		{
			vo.setPswd(DigestUtils.md5Hex(vo.getPswd()));			
		}
		dao.update(vo, id);
		return ;
	}
	
	@Override
	public void updatePswd(Integer userId, String pswd) throws ExecuteException
	{
		User vo = new User();
		vo.setPswd(DigestUtils.md5Hex(pswd));
		dao.update(vo, userId);
		return ;
	}

	@Override
	public void delete(Integer id) throws ExecuteException
	{
		dao.delete(id);
	}

	@Override
	public User findById(Integer id, Integer depth) throws ExecuteException
	{
		return dao.findById(id, depth);
	}

	@Override
	public PageList<User> findUserList(Integer areaCityId, Integer areaCountyId, String userName, Integer role, Integer pageNum)
			throws ExecuteException
	{
		return dao.findUserList(areaCityId, areaCountyId, userName, role, Depth.ONE, pageNum, Configs.USER_AMT_PER_PAGE, User.T_ID, true);
	}

	@Override
	public User findUniqueByName(String userName) throws ExecuteException
	{
		return dao.findByPropertyUnique(User.T_LOGIN_NAME, userName, Depth.ZERO);
	}
	
	@Override
	public PageList<User> findUserListInArea(Integer areaId, Integer pageNum) throws ExecuteException
	{
		Area area = DAOFactory.createAreaDAO().findById(areaId, Depth.ZERO);
		if(area == null)
		{
			return new PageList<User>();
		}
		String column = null;
		if(area.isProvince())
		{
			column = User.T_PROVINCE_ID;
		}
		else if(area.isCity())
		{
			column = User.T_CITY_ID;
		}
		else if(area.isCounty())
		{
			column = User.T_COUNTY_ID;
		}
		return dao.findByProperty(column, areaId, Depth.ZERO, pageNum, Configs.USER_AMT_PER_PAGE, null, null);
	}
	
	//
	// Utility
	//
	@Override
	public boolean isPswdMatch(Integer userId, String pswd) throws ExecuteException
	{
		if(pswd == null || userId == null)
		{
			return false;
		}
		//
		User user = DAOFactory.createUserDAO().findById(userId, Depth.ZERO);
		if(user == null)
		{
			return false;
		}
		// check pswd.
		if(user.getPswd().equals(DigestUtils.md5Hex(pswd)))
		{
			return true;
		}
		return false;
	}
	
	@Override
	public boolean hasUserInArea(Integer areaId) throws ExecuteException
	{
		return  this.findUserListInArea(areaId, null).isEmpty() == false;
	}

	private static final Pattern numRegex = Pattern.compile("\\d+");
	private static final Pattern wordRegex = Pattern.compile("\\w+");
	
	@Override
	public boolean isPswdValid(String pswd)
	{
		if(pswd.length() < 6 || pswd.length() > 30)
		{
			return false;
		}
		if(numRegex.matcher(pswd).find() && wordRegex.matcher(pswd).find())
		{
			return true;
		}
		return false;
	}
	
	
}
